<?php

/**
 * empleado actions.
 *
 * @package    courier
 * @subpackage empleado
 * @author     Your name here
 */
class empleadoActions extends sfActions
{
 
 public function executeIndex(sfWebRequest $request)
  {
    $this -> formulario = new IdentificaForm();
	$this -> setLayout('layoutEmpleado');  
	if ($request -> isMethod('post')){
	    $login = $request -> getParameter("login");
	    $password = $request -> getParameter("password");
	    $criteria = new Criteria();
	    $criteria -> add(EmpleadoPeer::EMP_LOGIN,$login);
	    $criteria -> add(EmpleadoPeer::EMP_PASS,$password);
	    $empleado = EmpleadoPeer::doSelectOne($criteria);
		if($empleado!=''){
		  $this->getUser()->setAuthenticated(true);
		  $this->getUser()->setAttribute('empleado_id',$empleado->getEmpId());
		  $this->getUser()->setAttribute('empleado_nombre',$empleado->getEmpNombre().' '.$empleado->getEmpApellido());
          if($empleado->getEmpPerfil()=='despachador')
			  $this -> redirect('empleado/despachador');
		  if($empleado->getEmpPerfil()=='recolector')
			  $this -> redirect('empleado/recolector');
		}
  	}
  }

  public function executeDespachador(sfWebRequest $request)
  {
	$this -> setLayout('layoutEmpleado');    
	$this->Empleado = EmpleadoPeer::retrieveByPk($request->getParameter('emp_id'));
  
  }

   public function executeSolicitudes(sfWebRequest $request)
  {
	$this -> setLayout('layoutEmpleado');
	$criteria = new Criteria();
	$criteria -> add(SolicitudPeer::SOL_EMPRECOLECTOR,NULL);	
 	$this->Solicitud = SolicitudPeer::doSelect($criteria);   
	 	
  } 

   public function executeSolicitudesrecolec(sfWebRequest $request)
  {
	$this -> setLayout('layoutEmpleado');
	$criteria = new Criteria();
	$criteria -> add(SolicitudPeer::SOL_ESTADO,'Recolectada');	
 	$this->Solicitud = SolicitudPeer::doSelect($criteria);   
	 	
  } 

  public function executeApoyo(sfWebRequest $request)
  {
	$this -> setLayout('layoutEmpleado');    
	$this-> Solicitud = SolicitudPeer::doSelect(new Criteria());
  	
  } 

   public function executeRecolector(sfWebRequest $request)
  {
    $this -> setLayout('layoutEmpleado');  
    $this->Empleado = EmpleadoPeer::retrieveByPk($request->getParameter('emp_id'));
  }

   public function executeSolicitudRecolector(sfWebRequest $request)
   {
	$this -> setLayout('layoutEmpleado');  	
    	$emp_id = $this->getUser()->getAttribute('empleado_id');
	$criteria = new Criteria();
	$criteria -> add(SolicitudPeer::SOL_EMPRECOLECTOR, $emp_id);	
	$this->Solicitud = SolicitudPeer::doSelect($criteria);   
    
   }

   public function executeLogout (sfWebRequest $request){
		$this->getUser()->setAuthenticated(false);
		$this->getUser()->getAttributeHolder()->clear();
		$this->redirect('empleado/index');
        $this -> setLayout('layoutEmpleado');     
   }

  public function executeShow(sfWebRequest $request)
  {
    $this -> setLayout('layoutEmpleado');  
    $this->Empleado = EmpleadoPeer::retrieveByPk($request->getParameter('emp_id'));
    $this->forward404Unless($this->Empleado);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new EmpleadoForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new EmpleadoForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
    
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($Empleado = EmpleadoPeer::retrieveByPk($request->getParameter('emp_id')), sprintf('Object Empleado does not exist (%s).', $request->getParameter('emp_id')));
    $this->form = new EmpleadoForm($Empleado);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($Empleado = EmpleadoPeer::retrieveByPk($request->getParameter('emp_id')), sprintf('Object Empleado does not exist (%s).', $request->getParameter('emp_id')));
    $this->form = new EmpleadoForm($Empleado);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($Empleado = EmpleadoPeer::retrieveByPk($request->getParameter('emp_id')), sprintf('Object Empleado does not exist (%s).', $request->getParameter('emp_id')));
    $Empleado->delete();

    $this->redirect('empleado/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $Empleado = $form->save();

      $this->redirect('empleado/edit?emp_id='.$Empleado->getEmpId());
    }
  }

   public function executeSimulation(sfWebRequest $request){

  }
}

